/**
 * 付款界面
 * author:zhangjlt
 * date:2017年3月16日13:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");
	var my = {
		data: {},
		wxUserInfoObj:{},
		hasMobile: false,
		//$(".mui-switch").is(":checked")
		/**
		 * bindEvent 页面事件
		 */
		bindEvent: function() {
			//返回
			$(".mui-switch").unbind().on("change", function() {
				if($(".mui-switch").is(":checked")) {
					//输入密码验证后记住密码
					$(".pwdPart_my").show();
					my.initPwdPart();	
				} else {
					//直接取消记住密码
					var 	isRemember="false";		
					var param = {
						"openId": window.localStorage.getItem("openId"),
						"appId": window.localStorage.getItem("appId"),
						"isRemember": isRemember
					};
					common.loadData({
						url: "/wxmember/service/isRememberPayPassword",
						param: param,
						successCallBack: function(res) {
							if(res.status == "0") {
								common.showTip({
									content: "修改成功"
								});
								my.wxUserInfoObj.data.isRememberPwd = false;
								var wxUserInfostr = JSON.stringify(my.wxUserInfoObj);
								window.localStorage.setItem("WxUserInfo", wxUserInfostr);
							} else {
								common.showTip({
									content: res.message
								}); //修改出错
								$(".mui-switch").prop("checked",true);
								
							}
						}, //回调函数
						cache: false //标识是不加载全部
					});
					
				}
			});
			$("#bind_mobile").unbind().on("click", function() {
				if(my.hasMobile) {
					return;
				} else {
					window.location.href = "#/index/register";
				}
			});
			$("#set_my").unbind().on("click", function() {
				window.localStorage.setItem("loadType","type0");
				window.location.href = "#/index/setPwd";
			});
			$("#reset_my").unbind().on("click", function() {
				window.localStorage.setItem("loadType","type3");
				window.location.href ="#/index/setPwd";
			});
			
			
		},
		
		/**
		 * 支付密码部分
		 */
		initPwdPart:function(){
			//弹出密码输入框
			$(".pwdPart_my").show();
			$("#hideinput_my").focus();
			$("#hideinput_my").unbind().on("input", function() {
				my.inputPwd($("#hideinput_my").val());
			});
			$(".pwdPart_my").unbind().on("click",function(){
				$("#hideinput_my").focus();
			});
			$(".pwd_fg_my").unbind().on("click",function(){
				window.localStorage.setItem("loadType","type3");
			    window.location.href = "#/index/setPwd";
			});
			$("#close_div_my").unbind().on("click",function(){
				$(".pwdPart_my").hide();
				$(".mui-switch").prop("checked",false);
				$("#hideinput_my").blur();
			});
			$("#Pwdmybtn").unbind().on("click",function(){
				//验证密码
				var str = $("#hideinput_my").val();
			   if(str &&str.length == 6) {
			   		var bool ="true";
					//验证密码				
					var param = {
						"openId": window.localStorage.getItem("openId"),
						"appId": window.localStorage.getItem("appId"),
						"payPassword":str,
						"isRemember":bool
					};
					common.loadData({
						url: "/wxmember/service/verifyPayPassword",
						param: param,
						successCallBack: function(res) {
							if(res.status == "0") {
								my.wxUserInfoObj.data.isRememberPwd = true;
								var wxUserInfostr = JSON.stringify(my.wxUserInfoObj);
								window.localStorage.setItem("WxUserInfo", wxUserInfostr);
								$("#hideinput_my").blur();
								$(".pwdPart_my").hide();
								common.showTip({
									content: "修改成功！"
								});
								//密码正确
//								window.location.href = "#/index/payto";
							} else {
								common.showTip({
									content: "密码输入错误，请重新输入！"
								}); //密码验证错误出错
								$("#hideinput_my").val("");
								$(".pwd_detail_my").text("");
							}
						}, //回调函数
						cache: false //标识是不加载全部
					});

				} else {
					common.showTip({
						content: "请输入六位数字密码！"
					});
				}
			});
		},
		//密码框显示*
		inputPwd:function(str){
			$(".pwd_detail_my").text("");
			for(var i = 0; i < str.length; i++) {
				$("#Pwdmy" + i).text("*");
			}
			if(str.length==6){
				$("#Pwdmybtn").click();
			}
		},
		/**
		 * 进行页面路由监听配置
		 * 
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				var nextPageFrom = "right"; //从右边过来
				//页面加载方向
				if(page_name == "nearShop" || page_name == "home" || page_name == "my") {
					nextPageFrom = "none";
				}
				//记录状态
				common.loadPage({
					pageUrl: "html/" + page_name + ".html",
					reload: true,
					from: nextPageFrom
				});
			});
			//公共页面监听路由
			Path.map("#home").to(function() {
				//返回首页
				common.loadPage({
					pageUrl: "html/index.html",
					reload: true,
					from: "none"
				});
			});
			Path.listen();
		},
		gatWechatInfo: function() {
			//			var appid = window.localStorage.getItem("appId");
			//			var openid = window.localStorage.getItem("openId");
			//			var param = {
			//
			//			};
			//			common.loadData({
			//				url: "/webchat/getuserinfo?appid=" + appid + "&openid=" + openid, //"/UMPPubServlet?method=getWxUserInfo",
			//				param: param,
			//				successCallBack: function(res) {
			//					if(res.status =="1"){
			var headImgUrl = window.localStorage.getItem("headImgUrl");
			$("#banner_my").attr("src", headImgUrl);
			$("#abbr_my").attr("src", headImgUrl);
			//					}
			//				}, //回调函数
			//				cache: false
			//			});

		},
		/**
		 * 页面数据加载
		 */
		initData: function() {
			my.gatWechatInfo();
			var wxUserInfostr = window.localStorage.getItem("WxUserInfo");
			if(wxUserInfostr) {

				var wxUserInfoObj = JSON.parse(wxUserInfostr);
				my.wxUserInfoObj = wxUserInfoObj;
				if(wxUserInfoObj.status == "0") {
					var mobile = wxUserInfoObj.data.mobile;
					if(mobile && mobile.length == 11) {
						$(".pr_next2_my").text(mobile);
						my.hasMobile = true;
					} else {
						$(".pr_next2_my").text("绑定手机号");
						my.hasMobile = false;
					}
					var isPayPwdEnable= wxUserInfoObj.data.isPayPwdEnable;
					if(isPayPwdEnable){
						$("#pwdPart_my").show();
					}else{
						$("#pwdPart_my").hide();
					}
					var hasPayPwd = wxUserInfoObj.data.hasPayPwd;
					if(hasPayPwd){
						$("#reset_my").show();
						$("#set_my").hide();
					}else{
						$("#reset_my").hide();
						$("#set_my").show();
					}
					var isRememberPwd =wxUserInfoObj.data.isRememberPwd;
					if(isRememberPwd){
						 $(".mui-switch").prop("checked",true);
					}else{
						 $(".mui-switch").prop("checked",false);
					}
					$(".user_name").text(wxUserInfoObj.data.name);
				}
			} else {
				common.showTip({
					content: "调用接口出错"
				});
			}
			

			//			var _this = this;
			//			var param = {};
			//			common.loadData({
			//				url: "/getmyListt",
			//				param: param,
			//				successCallBack: _this.getmyListCallBack, //回调函数
			//				dataSource: "getmyList"
			//			});

		},
		//callback function
		getmyListCallBack: function(json) {
			console.log("调用后台ajax返回值为：" + JSON.stringify(json));
			if(json.status == "0") {
				my.data = json;
				var html2 = template('myTemplate', my.data);
				document.getElementById('main_my').innerHTML = html2;
			} else {
				common.showTip({
					content: json.message
				});
			}

		},
		/**
		 * each page init mothed
		 */
		initialize: function() {
			//动态加载数据
			this.initData();
			//事件绑定
			this.bindEvent();
			//路由绑定
			this.bindRout();
		}
	};

	module.exports = my;
});